/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.citi.dahg.cve.controller.service.impl;

import com.citi.dahg.cve.controller.exceptions.SystemException;
import com.citi.dahg.cve.controller.model.Usuario;
import com.citi.dahg.cve.controller.service.BasicService;
import com.citi.dahg.cve.controller.service.local.UserService;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;

/**
 *
 * @author dh90940
 */
@Stateless
public class UsuarioServiceImpl extends BasicService<Usuario> implements UserService {

    @Override
    public Usuario login(String soeid, String pass) throws SystemException {
        String query = "select u from Usuario u where u.soeid=:soeid and u.pass=:pass";
        Usuario user = null;
        String passEncrypt=null;
        
        try {
            passEncrypt=getDecrypt().hash(pass);
        }
        catch (Exception ex) {
            throw new SystemException(ex);
        }
        
        try {
        user = (Usuario)getEntityManager().createQuery(query)
                .setParameter("soeid", soeid)
                .setParameter("pass", passEncrypt).getSingleResult();
        }
        catch (NoResultException re) {
            throw new SystemException("Usuario o password no valido");
        }
        return user;
    }

    @Override
    protected String queryGetAll() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected Class<Usuario> getClazz() {
        return Usuario.class;
    }

    
    
}
